//
//  MKUIBadgeView.m
//  MKUI
//
//  Created by moon on 15/10/28.
//  Copyright © 2015年 heimavista. All rights reserved.
//

#import "MKUIBadgeView.h"
#import <MKUI/MKUI.h>

@implementation MKUIBadgeViewBase

- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.userInteractionEnabled = NO;
        self.translatesAutoresizingMaskIntoConstraints = NO;
        //
        self.backgroundView = [[UIView alloc] init];
        self.backgroundView.backgroundColor = [UIColor redColor];
        self.backgroundView.clipsToBounds = YES;
        [self addSubview:self.backgroundView];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    [self layoutMyself];
    CGRect bounds = self.bounds;
    //
    CGRect f1 = bounds;
    self.backgroundView.frame = f1;
    CGFloat cornerRadius = MIN(f1.size.height,f1.size.width)/2.0;
    self.backgroundView.layer.cornerRadius = cornerRadius;
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGSize sizeFits = size;
    return sizeFits;
}
- (void)layoutMyself{
    CGRect badgeOwnerFrame = self.badgeOwner.mk_frameSafety;
    CGPoint badgeOffset = self.badgeOffset;
    //
    CGRect f1 = CGRectZero;
    f1.size = [self sizeThatFits:CGSizeMake(99999999, 99999999)];
    CGPoint center = CGPointZero;
    switch (self.badgePosition) {
        case MKUIBadgeViewPositionTopRight:
            center.x = CGRectGetMaxX(badgeOwnerFrame);
            center.y = badgeOwnerFrame.origin.y;
            break;
        case MKUIBadgeViewPositionTopLeft:
            center.x = badgeOwnerFrame.origin.x;
            center.y = badgeOwnerFrame.origin.y;
            break;
        case MKUIBadgeViewPositionBottomRight:
            center.x = CGRectGetMaxX(badgeOwnerFrame);
            center.y = CGRectGetMaxY(badgeOwnerFrame);
            break;
        case MKUIBadgeViewPositionBottomLeft:
            center.x = badgeOwnerFrame.origin.x;
            center.y = CGRectGetMaxY(badgeOwnerFrame);
            break;
        default:
            break;
    }
    center.x += badgeOffset.x;
    center.y += badgeOffset.y;
    MKCGRectSetMidX(&f1, center.x);
    MKCGRectSetMidY(&f1, center.y);
    self.mk_frameSafety = f1;
}
- (void)dealloc{
    
}
#pragma mark - protocol:+ (NSString *)stringWithUnreadCount:(NSInteger)count
#define kMKUIBadgeViewAnimateDuration 0.25
- (void)setBadge:(nullable NSString *)badge toView:(UIView *)view animated:(BOOL)animated{
    if(self.badgeOwner!=view){
        [self removeFromSuperview];
    }
    self.badgeOwner = view;
    UIView *containerView = view.superview;
    if(badge.length==0){
        if(animated){
            CGAffineTransform m = CGAffineTransformMakeScale(0.1, 0.1);
            [UIView animateWithDuration:kMKUIBadgeViewAnimateDuration animations:^{
                self.transform = m;
                self.alpha = 0;
            } completion:^(BOOL finished) {
                self.badgeString = badge;
                [self removeFromSuperview];
            }];
        }else{
            self.badgeString = badge;
            [self removeFromSuperview];
        }
    }else{
        self.badgeString = badge;
        if(animated){
            BOOL changeViewOnly = NO;
            if(self.superview!=containerView){//添加视图
                [containerView addSubview:self];
                [self setNeedsLayout];
                [self layoutIfNeeded];
                if(self.alpha==1){//渐现效果
                    self.alpha = 0;
                }
                changeViewOnly = NO;
            }else{//值变更
                changeViewOnly = YES;
            }
            if(!changeViewOnly){
                if(CGAffineTransformIsIdentity(self.transform)){
                    CGAffineTransform m = CGAffineTransformMakeScale(0.1, 0.1);
                    self.transform = m;
                }
            }
            [UIView animateWithDuration:kMKUIBadgeViewAnimateDuration animations:^{
                self.transform = CGAffineTransformIdentity;
                self.alpha = 1;
                if(changeViewOnly){
                    [self setNeedsLayout];
                    [self layoutIfNeeded];
                }else{
                    
                }
            } completion:^(BOOL finished) {
            }];
        }else{
            [containerView addSubview:self];
            [self setNeedsLayout];
        }
    }
}
- (void)refreshBadge{
    [self setNeedsLayout];
}
@end

@implementation MKUIBadgeTextView
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        //
        self.badgeLabel = [[UILabel alloc] init];
        self.badgeLabel.font = [UIFont systemFontOfSize:13];
        self.badgeLabel.textColor = [UIColor whiteColor];
        self.badgeLabel.textAlignment = NSTextAlignmentCenter;
        self.contentInsets = UIEdgeInsetsMake(4, 7, 4, 7);
        [self addSubview:self.badgeLabel];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect f1 = self.bounds;
    self.badgeLabel.frame = f1;
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGSize sizeFits = size;
    UIEdgeInsets insets = self.contentInsets;
    CGSize s1 = [self.badgeLabel sizeThatFits:CGSizeMake(99999999, 99999999)];
    sizeFits.width = s1.width+insets.left+insets.right;
    sizeFits.height = s1.height+insets.top+insets.bottom;
    sizeFits.width = MAX(sizeFits.width,sizeFits.height);
    return sizeFits;
}
- (void)setBadgeString:(NSString *)badgeString{
    [super setBadgeString:badgeString];
    self.badgeLabel.text = badgeString;
}
@end

@implementation MKUIBadgeDotView
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.badgeDotSize = CGSizeMake(10, 10);
    }
    return self;
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGSize sizeFits = self.badgeDotSize;
    return sizeFits;
}
@end
